﻿using DataSyncCommon;
using SupDataSync.ChildForms;
using SupDataSync.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SupDataSync
{
    /// <summary>
    /// 主界面
    /// </summary>
    public partial class FrmMain : Form
    {
        private int childFormNumber = 0;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            FrmSet childForm = new FrmSet();
            childForm.MdiParent = this;
            //childForm.Text = "窗口 " + childFormNumber++;
            childForm.Show();
            childForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            childForm.WindowState = FormWindowState.Maximized;
            string webApi = ConfigHelper.GetAppConfig("webapi") + "";
            string timeOutStr = ConfigHelper.GetAppConfig("timeout") + "";
            string tenantIdStr = ConfigHelper.GetAppConfig("tenantid") + "";

            SysConfig.TestApiUrl = webApi + ":5007/SupForOut/SupDataSync/Test";
            SysConfig.GetTenantApiUrl = webApi + ":5007/SupForOut/SupDataSync/GetTenantList";
            SysConfig.SyncBaseApiUrl = webApi + ":5007/SupForOut/SupDataSync/SyncBaseData";
            SysConfig.SyncBusinessApiUrl = webApi + ":5007/SupForOut/SupDataSync/SyncBusinessData";
            SysConfig.TimeOutSecond = 300;

            int timeout = 0;
            if (!string.IsNullOrEmpty(timeOutStr) && int.TryParse(timeOutStr, out timeout))
                SysConfig.TimeOutSecond = timeout;

            int tenantId = 0;
            if (!string.IsNullOrEmpty(timeOutStr) && int.TryParse(tenantIdStr, out tenantId))
                SysConfig.TenantId = tenantId;
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Form();
            childForm.MdiParent = this;
            childForm.Text = "窗口 " + childFormNumber++;
            childForm.Show();
        }

        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetMenu_Click(object sender, EventArgs e)
        {
            bool lb_avtive = false;
            foreach (Form childForm in MdiChildren)
            {
                if (childForm.Name == "FrmSet")
                {
                    childForm.Activate();
                    childForm.Show();
                    lb_avtive = true;
                }
            }
            if (!lb_avtive)
            {
                FrmSet childForm = new FrmSet();
                childForm.MdiParent = this;
                //childForm.Text = "窗口 " + childFormNumber++;
                childForm.Show();
                childForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
                childForm.WindowState = FormWindowState.Maximized;
            }
        }

        /// <summary>
        /// 基础数据同步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BaseDataSyncMenu_Click(object sender, EventArgs e)
        {
            if (!CheckConnnect())
                return;

            bool lb_avtive = false;
            foreach (Form childForm in MdiChildren)
            {
                if (childForm.Name == "FrmBaseDataSync")
                {
                    childForm.Activate();
                    childForm.Show();
                    lb_avtive = true;
                }
            }
            if (!lb_avtive)
            {
                FrmBaseDataSync childForm = new FrmBaseDataSync();
                childForm.MdiParent = this;
                //childForm.Text = "窗口 " + childFormNumber++;
                childForm.Show();
                childForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
                childForm.WindowState = FormWindowState.Maximized;
            }
        }

        /// <summary>
        /// 业务数据同步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BusinessDataSyncMenu_Click(object sender, EventArgs e)
        {
            if (!CheckConnnect())
                return;

            bool lb_avtive = false;
            foreach (Form childForm in MdiChildren)
            {
                if (childForm.Name == "FrmBusiDataSync")
                {
                    childForm.Activate();
                    childForm.Show();
                    lb_avtive = true;
                }
            }
            if (!lb_avtive)
            {
                FrmBusiDataSync childForm = new FrmBusiDataSync();
                childForm.MdiParent = this;
                //childForm.Text = "窗口 " + childFormNumber++;
                childForm.Show();
                childForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
                childForm.WindowState = FormWindowState.Maximized;
            }
        }

        /// <summary>
        /// 检测数据库连接
        /// </summary>
        /// <returns></returns>
        private bool CheckConnnect()
        {
            if(string.IsNullOrEmpty(SysConfig.ConnectionStr))
            {
                MessageBox.Show("请先设置连接本地数据库！");
                return false;
            }

            if(SysConfig.TenantId == 0)
            {
                MessageBox.Show("请先设置要导入的企业ID！");
                return false;
            }

            return true;
        }

        private void FromDataDelete_Click(object sender, EventArgs e)
        {
            //if (!CheckConnnect())
            //    return;

            bool lb_avtive = false;
            foreach (Form childForm in MdiChildren)
            {
                if (childForm.Name == "FrmDataDelte")
                {
                    childForm.Activate();
                    childForm.Show();
                    lb_avtive = true;
                }
            }
            if (!lb_avtive)
            {
                FrmDataDelte childForm = new FrmDataDelte();
                childForm.MdiParent = this;
                childForm.Show();
                childForm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
                childForm.WindowState = FormWindowState.Maximized;
            }
        }
    }
}
